import VueRouter from "vue-router";
import Vue from "vue";
import login from "@/views/login/login.vue";
import layout from "@/views/layout/layout.vue";
import chart from "@/views/layout/chart/chart.vue";
import subject from "@/views/layout/subject/subject.vue";
import questionList from "@/views/layout/questionList/questionList.vue";
import business from "@/views/layout/business/business.vue";
import userList from "@/views/layout/userList/userList.vue";
import Nprogress from "nprogress";
import "nprogress/nprogress.css";
Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/layout/subject",
      meta: {
        title: "数据概览",
      },
    },
    {
      path: "/login",
      component: login,
      meta: {
        title: "登录",
        roles: ["超级管理员", "初级管理员", "老师", "学生"],
      },
    },
    {
      path: "/layout",
      component: layout,
      children: [
        {
          path: "/layout/chart",
          component: chart,
          meta: {
            info: "欢迎来到首页",
            title: "数据概览",
            roles: ["超级管理员", "初级管理员", "老师"],
          },
        },
        {
          path: "/layout/subject",
          component: subject,
          meta: {
            info: "欢迎来到学科列表页",
            title: "学科列表",
            roles: ["超级管理员", "初级管理员", "老师", "学生"],
          },
        },
        {
          path: "/layout/questionList",
          component: questionList,
          meta: {
            info: "这里是题库列表页",
            title: "题库列表",
            roles: ["超级管理员", "初级管理员", "老师"],
          },
        },
        {
          path: "/layout/business",
          component: business,
          meta: {
            message: "这里是企业列表页",
            title: "企业列表",
            roles: ["超级管理员", "初级管理员", "老师"],
          },
        },
        {
          path: "/layout/userList",
          component: userList,
          meta: {
            info: "欢迎来到用户首页",
            title: "用户列表",
            roles: ["超级管理员", "初级管理员"],
          },
        },
      ],
    },
  ],
});
import { Message } from "element-ui";
import store from "@/store/store.js";
import { removeLocal } from "../utils/local";
// 路由前置守卫
// Nprogress进度条
router.beforeEach((to, from, next) => {
  // console.log("to", to); //去哪里
  // console.log("from", from); //来至哪里
  Nprogress.start(); //写在next上面
  if (to.meta.roles.includes(store.state.role)) {
    next(); //允许通过next(),不允许通过next(path路径)
  } else {
    Message.error("您无权访问该页面");
    removeLocal("token");
    next("/login");
  }
});
// 路由后置守卫
router.afterEach((to) => {
  Nprogress.done();
  document.title = to.meta.title;
  // setTimeout(() => {
  // }, 100000);设置定时器测试Nprogress.done()右上角的加载小圆圈
});
// router.afterEach((to, from) => {
//   console.log("to", to); //去哪里
//   console.log("from", from); //来至哪里
//   Nprogress.done();
// });
new Vue({
  router,
});

// 输出，将路由暴露出去
export default router;
